package com.dingreading.cloud.web.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dingreading.cloud.common.util.DateUtil;
import com.dingreading.cloud.common.util.NepUtil;
import com.dingreading.cloud.web.entity.NjTicketUser;
import com.dingreading.cloud.web.entity.table.NjTicketUserTableDef;
import com.dingreading.cloud.web.mapper.NjTicketUserMapper;
import com.dingreading.cloud.web.service.NjTicketUserService;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.row.Row;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2024-12-23
 */
@Service
public class NjTicketUserServiceImpl extends BaseServiceImpl<NjTicketUserMapper, NjTicketUser, NjTicketUserTableDef> implements NjTicketUserService {

    @Override
    protected NjTicketUserTableDef getTable() {
        return NjTicketUserTableDef.njTicketUser;
    }

    @Override
    public List<NjTicketUser> getByOpenid(String openid, String year) {
        return getMapper().selectListByCondition(table.openid.eq(openid).and(table.year.eq(year)));
    }

    @Override
    public List<NjTicketUser> getByPhone(String phone, String year) {
        return getMapper().selectListByCondition(table.phone.eq(phone).and(table.year.eq(year)));
    }

    @Override
    public NjTicketUser getByUidAndAddTime(String uid, Date date, String year) {
        return getMapper().selectOneByCondition(table.uid.eq(uid).and(table.addTime.eq(date)).and(table.year.eq(year)));
    }

    @Override
    public NjTicketUser getByUid(String uid, String year) {
        return getMapper().selectOneByCondition(table.uid.eq(uid).and(table.year.eq(year)));
    }

    @Override
    public boolean setPermitStatus(String uid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.uid.eq(uid))
                .and(table.getStatus.eq(1))
                .and(table.permitStatus.eq(0));
        return UpdateChain.of(NjTicketUser.class)
                .set(table.permitStatus, 1)
                .set(table.permitStatusName, "已领取")
                .set(table.permitTime, DateUtil.getNowDatetime())
                .where(condition)
                .update();
    }

    @Override
    public JSONObject getGetStatusCnt() {
        String year = String.valueOf(DateUtil.getYear());
        QueryWrapper wrapper = QueryWrapper.create()
                .select(QueryMethods.count(table.id).as("cnt"), table.getStatus)
                .from(table)
                .where(table.year.eq(year))
                .groupBy(table.getStatus);
        List<Row> rows = getMapper().selectRowsByQuery(wrapper);

        JSONObject jsonObject = new JSONObject();
        long unGetCnt = 0;
        long getCnt = 0;
        if (CollectionUtils.isNotEmpty(rows)) {
            for (Row row : rows) {
                int getStatus = NepUtil.nullToZero(row.getInt("get_status"));
                long cnt = NepUtil.nullToZero(row.getLong("cnt"));
                if (getStatus == 0)
                    unGetCnt = cnt;
                else
                    getCnt = cnt;
            }
        }
        jsonObject.put("getNum", getCnt);
        jsonObject.put("unGetNum", unGetCnt);
        return jsonObject;
    }

    @Override
    public List<String> listByPhoneAndYear(String year, List<String> phoneList) {
        QueryCondition condition = table.phone.in(phoneList).and(table.year.eq(year));
        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.phone)
                .from(table)
                .where(condition);
        return getMapper().selectObjectListByQueryAs(wrapper, String.class);
    }

    @Override
    public List<NjTicketUser> listByYearAndSource(String year, String source) {
        QueryCondition condition = table.source.eq(source).and(table.year.eq(year));
        return getMapper().selectListByCondition(condition);
    }

}
